package com.itboye.ihomebank;

import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.mobileim.IYWLoginService;
import com.alibaba.mobileim.IYWUIPushListener;
import com.alibaba.mobileim.YWAPI;
import com.alibaba.mobileim.YWIMKit;
import com.alibaba.mobileim.YWLoginParam;
import com.alibaba.mobileim.channel.event.IWxCallback;
import com.itboye.ihomebank.base.BaseFragment;
import com.itboye.ihomebank.bean.MessWeiDuBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.login.LoginActivity;
import com.itboye.ihomebank.message.ActivitySystemMessage;
import com.itboye.ihomebank.presenter.KeyPresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

/**
 * 消息
 * 
 */
public class MessageFragment extends BaseFragment implements OnClickListener,Observer{
	TextView add_shap_title_tv;
	ImageView close_icon, img_other;

	RelativeLayout contacts_system, contacts_qianyue, contacts_yuyue,
			contacts_mensuo;
	private String uid;
	
	private TextView contacts_xitong,contacts_qy,contacts_yy,contacts_ms;//显示消息的标签
	
	KeyPresenter keyPresenter;
	MessWeiDuBean bean;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		return inflater.inflate(R.layout.contacts_layout, null);

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public int initView() {
		return 0;
	}

	@Override
	public void onMyActivityCreated() {
		add_shap_title_tv.setText("消息");
		close_icon.setVisibility(View.GONE);
		img_other.setBackgroundResource(R.drawable.messgage_xiaoxi);
		img_other.setVisibility(View.VISIBLE);
		uid = SPUtils.get(getActivity(), null, SPContants.USER_ID, "") + "";
		contacts_xitong.setVisibility(View.GONE);
		contacts_qy.setVisibility(View.GONE);
		contacts_yy.setVisibility(View.GONE);
		contacts_ms.setVisibility(View.GONE);
		
		keyPresenter=new KeyPresenter(this);

	}
	@Override
	public void onResume() {
		super.onResume();
		keyPresenter.getWeiDu(uid);
	}

	// /消息类型【1=>系统消息，2=>预约消息,3=>签约消息,4=>智能锁消息】
	@Override
	public void onClick(View v) {
		Intent intent = null;
		uid = SPUtils.get(getActivity(), null, SPContants.USER_ID, "") + "";
		if (uid.equals("")) {
			intent = new Intent(getActivity(), LoginActivity.class);
			startActivity(intent);
			return;
		}
		switch (v.getId()) {
		case R.id.contacts_system:// 系统消息
			intent = new Intent(getActivity(), ActivitySystemMessage.class);
			intent.putExtra("type", 1);
			intent.putExtra("title", "系统消息");
			startActivity(intent);
			break;
		case R.id.contacts_qianyue:// 签约消息
			intent = new Intent(getActivity(), ActivitySystemMessage.class);
			intent.putExtra("type", 3);
			intent.putExtra("title", "签约消息");
			startActivity(intent);
			break;
		case R.id.contacts_yuyue:// 
			intent = new Intent(getActivity(), ActivitySystemMessage.class);
			intent.putExtra("type", 2);intent.putExtra("title", "预约消息");
			startActivity(intent);
			break;
		case R.id.contacts_mensuo:// 门锁消息
			intent = new Intent(getActivity(), ActivitySystemMessage.class);
			intent.putExtra("type", 4);intent.putExtra("title", "门锁消息");
			startActivity(intent);
			break;
		case R.id.img_other:
			// 此实现不一定要放在Application onCreate中
			final String userid = SPUtils.get(getActivity(), null,
					SPContants.BaichuanUID, "") + "";
			// final String userid =
			// "tbimuser_8f85517967795eeef66c225f7883bdcb";
			// 此对象获取到后，保存为全局对象，供APP使用
			// 此对象跟用户相关，如果切换了用户，需要重新获取
			final YWIMKit mIMKit = YWAPI.getIMKitInstance(userid,
					
					SPContants.YW_APPKEY);
			mIMKit.unRegisterPushListener(new IYWUIPushListener() {
				
				@Override
				public void onMessageComing() {
					// TODO Auto-generated method stub
					
				}
			});
			// 开始登录
			String password = "itboye";
			IYWLoginService loginService = mIMKit.getLoginService();
			YWLoginParam loginParam = YWLoginParam.createLoginParam(userid,
					password);
			loginService.login(loginParam, new IWxCallback() {

				@Override
				public void onSuccess(Object... arg0) {
					Intent intent = mIMKit.getConversationActivityIntent();// 获取会话列表
					startActivity(intent);
				}

				@Override
				public void onProgress(int arg0) {
					// TODO Auto-generated method stub
				}

				@Override
				public void onError(int errCode, String description) {
					ByAlert.alert(errCode + ":" + description);// 如果登录失败，errCode为错误码,description是错误的具体描述信息
				}
			});
			break;

		default:
			break;
		}

	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		// ByAlert.alert(entity.getData());
		if (entity != null) {
			
			if (entity.getEventType() == KeyPresenter.weidu_success) {
				bean=(MessWeiDuBean) entity.getData();
				
				if(bean.getSystem()>0){
					contacts_xitong.setVisibility(View.VISIBLE);
					contacts_xitong.setText(bean.getSystem()+"");
				}
				if(bean.getOrder()>0){
					
					contacts_yy.setVisibility(View.VISIBLE);
					contacts_yy.setText(bean.getSign()+"");
				}
				if(bean.getSign()>0){
					contacts_qy.setVisibility(View.VISIBLE);
					contacts_qy.setText(bean.getOrder()+"");
				}
				if(bean.getLock()>0){
					contacts_ms.setVisibility(View.VISIBLE);
					contacts_ms.setText(bean.getLock()+"");
				}
				
			}
		}
		
	}
}
